package com.tech.connect.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.ksy.common.utils.LogTool;
import com.ksy.map.GDMapView;
import com.tech.connect.R;
import com.tech.connect.activity.CityChooseActivity;
import com.tech.connect.activity.EmptyActivity;
import com.tech.connect.activity.ModelMainActivity;
import com.tech.connect.activity.PublishBaoWuActivity;
import com.tech.connect.activity.PublishFuWuActivity;
import com.tech.connect.activity.PublishLvYouPeiActivity;
import com.tech.connect.api.CurrentInfo;
import com.tech.connect.dialog.PublishDialog;
import com.tech.connect.zhaorencai.zhuanwaikuai.PublishBiaoActivity;

public class MainTabHomeFragment extends BaseFragment {

    private AMap aMap;
    private GDMapView mapView;
    private Marker marker;
    private LatLng currentLocation;
    private TextView tvAddress;
    private TextView tvCity;
    private AMapLocationClient locationClient;

    @Override
    public View onCreateView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_main_tab_home, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initLocation();
        loadData();
    }

    private void initLocation() {
        locationClient = new AMapLocationClient(activity);
        AMapLocationClientOption locationOption = new AMapLocationClientOption();
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        locationOption.setGpsFirst(false);//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        locationOption.setHttpTimeOut(30 * 1000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        locationOption.setInterval(10 * 60 * 1000);//可选，设置定位间隔。默认为2秒
        locationOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是true
        locationOption.setOnceLocation(true);//可选，设置是否单次定位。默认是false
        locationOption.setOnceLocationLatest(false);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        locationOption.setSensorEnable(false);//可选，设置是否使用传感器。默认是false
        locationOption.setWifiScan(true); //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        locationOption.setLocationCacheEnable(true); //可选，设置是否使用缓存定位，默认为true
        locationClient.setLocationOption(locationOption);
        AMapLocationListener locationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation location) {
                LogTool.w("AMapLocation " + location);
                if (location != null && location.getErrorCode() == 0) {
                    currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
                    CurrentInfo.getUserInfo().lat = location.getLatitude();
                    CurrentInfo.getUserInfo().lng = location.getLongitude();
                    CurrentInfo.getUserInfo().provinceName=location.getProvince();
                    CurrentInfo.getUserInfo().cityName=location.getCity();
                    CurrentInfo.getUserInfo().district=location.getDistrict();
                    CurrentInfo.getUserInfo().address=location.getAddress();
                } else {
                    currentLocation = null;
                }
                tvCity.setText(  CurrentInfo.getUserInfo().cityName);
                tvAddress.setText(CurrentInfo.getUserInfo().address);
                if (mapView != null && currentLocation != null) {
                    if (marker == null) {
                        marker = mapView.showMarker(currentLocation);
                    } else {
                        marker.setPosition(currentLocation);
                    }
                    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 12));
                }
            }
        };
        // 设置定位监听
        locationClient.setLocationListener(locationListener);
        locationClient.startLocation();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getView() != null) {
            mapView = getView().findViewById(R.id.mapView);
            if (mapView != null) {
                mapView.onCreate(savedInstanceState);
                aMap = mapView.getMap();

                aMap.getUiSettings().setZoomControlsEnabled(false);
                aMap.getUiSettings().setMyLocationButtonEnabled(false);//设置默认定位按钮是否显示，非必需设置。
                aMap.setMyLocationEnabled(false);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。

                if (currentLocation != null) {
                    if (marker == null) {
                        marker = mapView.showMarker(currentLocation);
                    } else {
                        marker.setPosition(currentLocation);
                    }
                }
            }
        }
    }

    private void initView(View view) {
        tvCity = view.findViewById(R.id.tvCity);
        tvCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(CityChooseActivity.class);
            }
        });
        View vUser = view.findViewById(R.id.vUser);
        View ivGps = view.findViewById(R.id.ivGps);
        ivGps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (locationClient != null) {
                    locationClient.startLocation();
                }
            }
        });
        View vUserDot = view.findViewById(R.id.vUserDot);
        View vMessage = view.findViewById(R.id.vMessage);
        View vMessageDot = view.findViewById(R.id.vMessageDot);
        vUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(EmptyActivity.class);
            }
        });
        vMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(EmptyActivity.class);
            }
        });
        View tvLookUser = view.findViewById(R.id.tvLookUser);
        tvLookUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModelMainActivity.jump(activity, 0);
            }
        });
        View tvLookServer = view.findViewById(R.id.tvLookServer);
        tvLookServer.setSelected(true);
        tvLookServer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModelMainActivity.jump(activity, 1);
            }
        });
        View tvLookFound = view.findViewById(R.id.tvLookFound);
        tvLookFound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModelMainActivity.jump(activity, 2);
            }
        });
        tvAddress = view.findViewById(R.id.tvAddress);
        View tvPublish = view.findViewById(R.id.tvPublish);
        tvPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PublishDialog publishDialog = new PublishDialog(activity, new PublishDialog.PublishListener() {
                    @Override
                    public void publishFuWu(PublishDialog dialog) {
                        dialog.dismiss();
                        jump2Activity(PublishFuWuActivity.class);
                    }

                    @Override
                    public void publishLvYou(PublishDialog dialog) {
                        dialog.dismiss();
                        jump2Activity(PublishLvYouPeiActivity.class);
                    }

                    @Override
                    public void publishZhaoBiao(PublishDialog dialog) {
                        dialog.dismiss();
                        jump2Activity(PublishBiaoActivity.class);
                    }

                    @Override
                    public void publishBaoWu(PublishDialog dialog) {
                        dialog.dismiss();
                        jump2Activity(PublishBaoWuActivity.class);
                    }

                    @Override
                    public void close(PublishDialog dialog) {
                        dialog.dismiss();
                    }
                });
                publishDialog.show();
            }
        });
    }

    private void loadData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (mapView != null) {
            mapView.onResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mapView != null) {
            mapView.onPause();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mapView != null) {
            mapView.onSaveInstanceState(outState);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mapView != null) {
            mapView.onDestroy();
        }
        if (locationClient != null) {
            locationClient.stopLocation();
            locationClient.onDestroy();
        }
        locationClient = null;
    }
}
